package com.jyxd.web.task;

import com.jyxd.web.dao.patient.PatientDao;
import com.jyxd.web.data.basic.QuartzTask;
import com.jyxd.web.data.basic.QuartzTime;
import com.jyxd.web.data.patient.Patient;
import com.jyxd.web.service.assessment.basic.QuartzTaskService;
import com.jyxd.web.service.assessment.basic.QuartzTimeService;
import com.jyxd.web.service.hisview.HisViewService;
import com.jyxd.web.util.CompareTimeUtil;
import com.jyxd.web.util.SpringUtil;
import com.jyxd.web.util.UUIDUtil;
import org.quartz.CronExpression;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定时同步HIS患者基本表
 */
public class HISQualityControlJob  implements Job {

    private static Logger logger= LoggerFactory.getLogger(HISQualityControlJob.class);
    //定义一个成员变量
    private static QuartzTask quartzTask;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            logger.info("开始同步His患者质控指标数据================");
            QuartzTime quartzTime=new QuartzTime();
            quartzTime.setStartTime(new Date());

            ServletContext context = null;
            //获取service
            context = (ServletContext) jobExecutionContext.getScheduler().getContext()
                    .get(QuartzServletContextListener.MY_CONTEXT_NAME);
            WebApplicationContext cxt = (WebApplicationContext) context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
            QuartzTimeService quartzTimeService = (QuartzTimeService) cxt.getBean("quartzTimeService");
            QuartzTaskService quartzTaskService = (QuartzTaskService) cxt.getBean("quartzTaskService");

            if(quartzTask==null){
                //为空时 表明第一次执行该方法 需要查询赋值
                Map<String,Object> map=new HashMap<>();
                map.put("jobName","HISQualityControlJob");
                map.put("jobGroup","HISQualityControlJob");
                quartzTask =quartzTaskService.queryDataByNameAndGroup(map);
            }
            PatientDao patientDao = SpringUtil.getBean(PatientDao.class);
            HisViewService hisViewService = SpringUtil.getBean(HisViewService.class);
            //查询本地的患者数据
            List<Patient> patientList = patientDao.getPatientAllList();
            //Patient patient = patientDao.queryData("5dabfa722bbf4d2d9ce06434494d9972");
            int sum=0;
            int count=0;
            if(patientList!=null && patientList.size()>0) {
                sum = patientList.size();
                //如果HIS数据库有数据更新，没有数据新增
                for (int i = 0; i <patientList.size() ; i++) {
                    hisViewService.handleDcIcuDatBrxxjlk(patientList.get(i));
                    count=i;
                }
               // for (Patient patient : patientList) {
               //     hisViewService.handleDcIcuDatBrxxjlk(patient);
                //    count=patientList[i]
               // }
            }
            CronExpression expression = new CronExpression(quartzTask.getCron());
            quartzTime.setNextTime(expression.getNextValidTimeAfter(quartzTime.getStartTime()));
            quartzTime.setQuartzTaskId(quartzTask.getId());
            quartzTime.setId(UUIDUtil.getUUID());
            quartzTime.setEndTime(new Date());
            quartzTime.setTimeLength(String.valueOf(CompareTimeUtil.calculatetimeGapSecond(quartzTime.getStartTime(),quartzTime.getEndTime())));
            quartzTime.setStatus(1);
            quartzTimeService.insert(quartzTime);
            logger.info("同步His患者基本信息完毕=============="+count);
        }catch (Exception e){
            logger.info("异常："+e);
        }
    }
}
